package org.quipu.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import org.quipu.client.events.AppEvent;
import org.quipu.client.events.AppEventBus;
import org.quipu.client.events.AppEventListener;
import org.quipu.client.events.GotoPageEvent;
import org.quipu.client.events.LoginEvent;
import org.quipu.dto.UserDto;

public class LoginPanel extends AppEventWidget implements AppEventListener {

    private final VerticalPanel vPanel;
    private final TextBox loginField;
    private final PasswordTextBox passwordField;
    private Label errorLabel;
    private FlexTable form;

    public LoginPanel(final AppEventBus eventBus, AppCache cache) {
        super(eventBus, cache);

        loginField = new TextBox();
        loginField.addStyleName("titleInput");
        passwordField = new PasswordTextBox();
        passwordField.addStyleName("titleInput");
        passwordField.addKeyPressHandler(new KeyPressHandler() {

            public void onKeyPress(KeyPressEvent event) {
                if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER) {
                    signIn();
                }
            }
        });
        errorLabel = new Label();
        errorLabel.setVisible(false);

        form = new FlexTable();
        form.setCellSpacing(0);
        form.setCellPadding(3);
        form.setWidget(0, 0, new Label("Login"));
        form.setWidget(0, 1, loginField);
        form.setWidget(1, 0, new Label("Password"));
        form.setWidget(1, 1, passwordField);
        form.setWidget(2, 0, errorLabel);
        form.getFlexCellFormatter().setColSpan(2, 0, 2);
        Button signInButton = new Button("Sign in", new ClickHandler() {
            public void onClick(ClickEvent event) {
                signIn();
            }
        });
        signInButton.setFocus(true);
        form.setWidget(3, 0, signInButton);
        form.getFlexCellFormatter().setColSpan(3, 0, 2);
        form.getFlexCellFormatter().setHorizontalAlignment(3, 0, HasHorizontalAlignment.ALIGN_CENTER);

        vPanel = new VerticalPanel();
        vPanel.addStyleName("contentPanel");
        vPanel.addStyleName("controlPanel");
        form.addStyleName("gridPanel");
        vPanel.add(form);
    }

    private void signIn() {
        getMainService().authenticate(loginField.getText(), passwordField.getText(), new AsyncCallback<UserDto>() {
            public void onFailure(Throwable caught) {
                errorLabel.setVisible(true);
                errorLabel.setText(caught.getMessage());
            }

            public void onSuccess(UserDto user) {
                errorLabel.setVisible(false);
                getEventBus().fireEvent(new LoginEvent(LoginPanel.this, user));
            }
        });
    }

    @Override
    public void init() {
        getEventBus().addListener(GotoPageEvent.class, this);
    }

    @Override
    public Widget getWidget() {
        return vPanel;
    }

    public void handleEvent(AppEvent event) {
        if(event != null) {
            if(event instanceof GotoPageEvent && GotoPageEvent.Page.LOGIN.equals(((GotoPageEvent) event).getObject())) {
                loginField.setFocus(true);
            }
        }
    }
}
